//
//  YZPopupCustomView.m
//  VET
//
//  Created by 刘文靖 on 2018/7/12.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPopupCustomView.h"
#import <YYKit/UIControl+YYAdd.h>
@interface YZPopupConfrimCustomView()
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *message;
@property (nonatomic, strong) NSString *confirmTitle;
@property (nonatomic, strong) NSString *cancelTitle;
@end
@implementation YZPopupConfrimCustomView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.size = CGSizeMake(300, 156);
    }
    return self;
}

- (void)setupUIWithTitle:(NSString *)title
                 message:(NSString *)message
            confirmTitle:(NSString *)confirmTitle
             cancelTitle:(NSString *) cancelTitle
          confirmHandler:(void (^)(void))confirmHandler
           cancelHandler:(void (^)(void))cancelHandler{
    
    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:message attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:[UIColor grayColor]}];
    NSAttributedString *attributedConfirmTitle = [[NSAttributedString alloc] initWithString:confirmTitle attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:[UIColor grayColor]}];
    NSAttributedString *attributedCancelTitle = [[NSAttributedString alloc] initWithString:cancelTitle attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
    [self setupUIWithAttributedTitle:nil attributedmessage:attributedMessage attributedconfirmTitle:attributedConfirmTitle attributedcancelTitle:attributedCancelTitle confirmHandler:confirmHandler cancelHandler:cancelHandler];
}

- (void)setupMessage:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmHandler:(void (^)(void))confirmHandler {
    
    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:message attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:[UIColor grayColor]}];
    NSAttributedString *attributedConfirmTitle = [[NSAttributedString alloc] initWithString:confirmTitle attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14],NSForegroundColorAttributeName:[UIColor grayColor]}];

    self.backgroundColor = kColorWithHex(0xffffff);
    
    UILabel *messageLabel = [[UILabel alloc] init];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.attributedText = attributedMessage;
    messageLabel.numberOfLines = 0;
    [self addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.right.offset(-24);
        make.top.offset(30);
    }];


    UIView *bottomlineView = [[UIView alloc]init];
    bottomlineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
    [self addSubview:bottomlineView];
    [bottomlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(1);
        make.bottom.offset(-54);
    }];

    UIButton *confirmButton = [[UIButton alloc] init];
    [confirmButton setAttributedTitle:attributedConfirmTitle forState:UIControlStateNormal];
    [self addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.top.mas_equalTo(bottomlineView.mas_bottom);

    }];
    [confirmButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        confirmHandler();
    }];
}

- (void)setupUIWithAttributedTitle:(NSAttributedString *)title
                 attributedmessage:(NSAttributedString *)message
            attributedconfirmTitle:(NSAttributedString *)confirmTitle
             attributedcancelTitle:(NSAttributedString *) cancelTitle
                    confirmHandler:(void (^)(void))confirmHandler
                     cancelHandler:(void (^)(void))cancelHandler{
    self.backgroundColor = kColorWithHex(0xffffff);
    if (title){
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 25)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.attributedText = title;
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 25, self.width, 1)];
        lineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
        lineView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
        [self addSubview:lineView];
    }
    
    UILabel *messageLabel = [[UILabel alloc] init];
    messageLabel.textAlignment = NSTextAlignmentCenter;
    messageLabel.attributedText = message;
    messageLabel.numberOfLines = 0;
    [self addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.right.offset(-24);
        make.top.offset(30);
    }];
    
    
    UIView *bottomlineView = [[UIView alloc]init];
    bottomlineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
    [self addSubview:bottomlineView];
    [bottomlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(1);
        make.bottom.offset(-54);
    }];
    
    UIButton *cancelButton= [[UIButton alloc] init];
    [cancelButton setAttributedTitle:cancelTitle forState:UIControlStateNormal];
    [self addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.top.mas_equalTo(bottomlineView.mas_bottom);
    }];
    [cancelButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        cancelHandler();
    }];
    
    UIView *middlelineView = [[UIView alloc]init];
    middlelineView.backgroundColor = kColorWithHexAlpha(0xD8D8D8,0.3);
    [self addSubview:middlelineView];
    [middlelineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(cancelButton.mas_right);
        make.width.offset(1);
        make.top.bottom.mas_equalTo(cancelButton);
    }];
    
    UIButton *confirmButton = [[UIButton alloc] init];
    [confirmButton setAttributedTitle:confirmTitle forState:UIControlStateNormal];
    [self addSubview:confirmButton];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(0);
        make.top.width.mas_equalTo(cancelButton);
        make.left.mas_equalTo(middlelineView.mas_right);
    }];
    [confirmButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        confirmHandler();
    }];
    
    
}

@end
